﻿// /*
//  * @Author: wanyiming
//  * @Date:2024-05-25-20:55
//  * @Description:
//  */

using System;
using Code.GameLogic.BaseClass;
using Code.GameLogic.Common;
using Code.GameLogic.Map;
using UnityEngine;

namespace Code.GameLogic.Monster.Controller.MonsterBehavior
{
    public class MoveComponent : AbsGameLogicComponent<MonsterController>
    {


        private WayPointBo _thisWayPointBo;
        private WayPointBo _nextWayPointBo;

        public float speed ; // 移动速度
        
        public override void InitComponent()
        {
            speed = ThisController.bo.Speed;
            _thisWayPointBo = WayPointManager.GetWayPointBo(gameObject.transform.position);
            _nextWayPointBo = WayPointManager.GetLastWayPoint(_thisWayPointBo.NextWayPointOrder);
            
        }


        public void Update()
        {
            MoveBehavior();
        }

        private void MoveBehavior()
        {
            if (_nextWayPointBo != null)
            {
                Vector3 direction = _nextWayPointBo.Postion - transform.position;
                float distance = direction.magnitude;

                if (distance > 0.05f)
                {
                    direction.Normalize();
                    transform.Translate(direction * speed * Time.deltaTime);
                }
                else
                {
                    // 到达目标位置后执行的操作
                    _thisWayPointBo = _nextWayPointBo;
                    _nextWayPointBo = WayPointManager.GetLastWayPoint(_nextWayPointBo.NextWayPointOrder);                }
            }

    
        }
        
        
    }
}